function addTraceList(point,longitude,latitude,currentTime,userId,routeId){
  var that = point;
  var newarray=[{
    longitude:longitude,
    latitude:latitude,
    theFirstDate:currentTime,
    userId:userId,
    routeId:routeId,
  }];
  that.data.traceList=that.data.traceList.concat(newarray);
}
var EARTH_RADIUS = 6378136.49;
function rad(d) {
  return d * Math.PI / 180.0;
}  
    //计算两点之间的坐标
    //function getDistance(lat1, lon1, lat2, lon2) {
      function getDistance(latFrom, lngFrom, latTo, lngTo){
      var radLatFrom = rad(latFrom);
      var radLatTo = rad(latTo);
      var a = radLatFrom - radLatTo;
      var b = rad(lngFrom) - rad(lngTo);
      var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLatFrom) * Math.cos(radLatTo) * Math.pow(Math.sin(b / 2), 2)));
      distance = distance * EARTH_RADIUS;
      distance = Math.round(distance * 10000) / 10000;
      return parseFloat(distance.toFixed(0));
      // var dis = 0;
      // var radLat1 = toRadians(lat1);
      // var radLat2 = toRadians(lat2);
      // var deltaLat = radLat1 - radLat2;
      // var deltaLng = toRadians(lon1) - toRadians(lon2);
      // var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
      // //6378137 地球半径
      // return dis * 6378137;

      // function toRadians(d) { return d * Math.PI / 180; }
  }
  //请求地理位置 
  function requestLocation (point) {
    var that = point;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
        that.moveTolocation();
      },
    })
  }
/**
   * 
   * 逆地址解析
   */
 function regeocodingAddress (point,qqmapsdk) {
  var that = point;
  //不在发布页面，不进行逆地址解析，节省调用次数，腾讯未申请额度前一天只有10000次
  // if (!that.data.showConfirm) {
  //   return;
  // }
  //通过经纬度解析地址
  qqmapsdk.reverseGeocoder({
    location: {
      latitude: that.data.centerLatitude,
      longitude: that.data.centerLongitude
    },
    success: function (res) {
      console.log('经纬度解析地址'+res);
      that.setData({
        centerAddressBean: res.result,
        selectAddress: res.result.formatted_addresses.recommend,
        currentProvince: res.result.address_component.province,
        currentCity: res.result.address_component.city,
        currentDistrict: res.result.address_component.district,
      })
    },
    fail: function (res) {
      console.log('经纬度解析地址2'+res);
    }
  });
}
//定位权限处理逻辑
function scopeSetting (point) {
    var that = point;
    wx.getSetting({
      success(res) {
        //地理位置
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success(res) {
              that.initMap();
            },
            fail() {
              wx.showModal({
                title: '提示',
                content: '定位失败，你未开启定位权限，点击开启定位权限 ',
                success: function (res) {
                  if (res.confirm) {
                    //打开设置页面
                    wx.openSetting({
                      success: function (res) {
                        if (res.authSetting['scope.userLocation']) {
                          that.initMap();
                        } else {
                          //不同意
                          wx.redirectTo({
                            url: '/pages/index/index'
                          })
                        }
                      }
                    })
                  }else if (res.cancel) {
                    wx.redirectTo({
                      url: '/pages/index/index'
                    })
                  }
                }
              })
            }
          })
        } else {
          that.initMap();
        }
      }
    })
  }
  function  getBackgroundLocationRight(point) {
    var that = point;
    wx.startLocationUpdateBackground({
      success(res) {
        that.initMap();
      },
      fail(res) {
        wx.showModal({
          title: '提示',
          content: '为了确保你退出小程序后运动信息不会丢失，请在位置信息处选择 "使用小程序期间和离开小程序后" ',
          success: function (res) {
            if (res.confirm) {
              //打开设置页面
              wx.openSetting({
                success: function (res) {
                  if (res.authSetting['scope.userLocationBackground']) {
                    that.initMap();
                  } else {
                    //不同意
                    wx.redirectTo({
                      url: '/pages/scenic-map/scenic-map'
                    })
                  }
                }
              })
            }else if (res.cancel) {
              wx.redirectTo({
                url: '/pages/scenic-map/scenic-map'
              })
            }
          }
        })
      }
    })

}
/**
 * 版本更新
 */
function checkUpdate() {
    if (wx.canIUse('getUpdateManager')) {
        const updateManager = wx.getUpdateManager();
        updateManager.onCheckForUpdate(function (res) {
            // 请求完新版本信息的回调
            // consoleUtil.log('onCheckForUpdate----------------->');
            // consoleUtil.log(res.hasUpdate);
        })

        updateManager.onUpdateReady(function () {
            wx.showModal({
                title: '更新提示',
                content: '新版本已经准备好，即刻体验？',
                success: function (res) {
                    if (res.confirm) {
                        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                        updateManager.applyUpdate();
                    }
                }
            })
        })

        updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
        })
    }
}

module.exports = {
    checkUpdate: checkUpdate,
    scopeSetting:scopeSetting,
    regeocodingAddress:regeocodingAddress,
    requestLocation:requestLocation,
    getBackgroundLocationRight:getBackgroundLocationRight,
    getDistance:getDistance,
    addTraceList:addTraceList
}
